package com.meizi.mall.product.service.impl;

import com.meizi.core.tool.exception.BizException;
import com.meizi.mall.common.status.ProductStatus;
import com.meizi.mall.product.entity.BrandEntity;
import com.meizi.mall.product.entity.CategoryBrandRelationEntity;
import com.meizi.mall.product.entity.CategoryEntity;
import com.meizi.mall.product.service.BrandService;
import com.meizi.mall.product.service.CategoryBrandRelationService;
import com.meizi.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meizi.core.tool.api.PageUtils;
import com.meizi.core.tool.api.Query;

import com.meizi.mall.product.dao.CategoryBrandRelationDao;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    BrandService brandService;
    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils catelogList(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils catelogList(Map<String, Object> params, Long brandId) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );

        return new PageUtils(page);

    }

    @Override
    public void saveByIds(CategoryBrandRelationEntity categoryBrandRelation) {
        QueryWrapper<CategoryBrandRelationEntity> eq = new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", categoryBrandRelation.getBrandId())
                .eq("catelog_id", categoryBrandRelation.getCatelogId());
        Integer integer = baseMapper.selectCount(eq);
        if(integer>1){
            throw new BizException(ProductStatus.IS_EXIST_RELATION);
        }

        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getBrandName());

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        save(categoryBrandRelation);
    }

    @Override
    public PageUtils brandsList(Map<String, Object> params, Long catId) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId)
        );

        return new PageUtils(page);
    }

}